package com.qianduo.struts.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.qianduo.service.impl.QianduoAgencyServiceImpl;
import com.qianduo.struts.AbstractAction;
import com.qianduo.util.Constants;
import com.qianduo.vo.QianduoAgencyForm;
import common.page.PageList;
import common.page.Pagination;
import common.util.StringUtil;

public class QianduoAgencyAction extends AbstractAction {
	public ActionForward addAgency(ActionMapping mapping,ActionForm form,HttpServletRequest req,HttpServletResponse res){
		//QianduoAgencyForm qianduoAgencyForm	= (QianduoAgencyForm)form;
		
		return mapping.findForward("addAgency");
	}
	
	public ActionForward listAgency(ActionMapping mapping,ActionForm form,HttpServletRequest req,HttpServletResponse res){
		try{
			//QianduoAgencyForm qianduoAgencyForm	= (QianduoAgencyForm)form;
			QianduoAgencyServiceImpl service	= (QianduoAgencyServiceImpl)getBean("qianduoAgencyService");
			PageList pageList	= Pagination.getPageList(req);
			List<QianduoAgencyForm> list	= service.listAgency(null, null, null, null, null, null, null, null, null, null, null, null, null, null, pageList);
			
			req.setAttribute("list", list);
		}catch(Exception e){
			Constants.log.error(className+" listAgency",e);
		}
		return mapping.findForward("listAgency");
	}
	
	public ActionForward updateAgency(ActionMapping mapping,ActionForm form,HttpServletRequest req,HttpServletResponse res){
		try{
			QianduoAgencyForm qianduoAgencyForm	= (QianduoAgencyForm)form;
			QianduoAgencyServiceImpl service	= (QianduoAgencyServiceImpl)getBean("qianduoAgencyService");
			PageList pageList	= Pagination.getPageList(req);
			List<QianduoAgencyForm> list		= service.listAgency(null, qianduoAgencyForm.getUsername(), null, null, null, null, null, null, null, null, null, null, null, null, pageList);
			if(null != list && list.size() > 0){
				qianduoAgencyForm				= list.get(0);
			}
		}catch(Exception e){
			Constants.log.error(className+" updateAgency",e);
		}
		return mapping.findForward("addAgency");
	}
	
	public ActionForward saveOrUpdateAgency(ActionMapping mapping,ActionForm form,HttpServletRequest req,HttpServletResponse res){
		QianduoAgencyForm qianduoAgencyForm	= (QianduoAgencyForm)form;		
		List<String> errors	= new ArrayList<String>();
		if(null == qianduoAgencyForm.getId() || qianduoAgencyForm.getId() == 0){
			qianduoAgencyForm.setId(null);
		}
		if(StringUtil.isEmpty(qianduoAgencyForm.getName())){
			errors.add("名称不能为空");
		}
		if(StringUtil.isEmpty(qianduoAgencyForm.getUsername())){
			errors.add("用户名不能为空");
		}
		if(StringUtil.isEmpty(qianduoAgencyForm.getPassword())){
			qianduoAgencyForm.setPassword(StringUtil.encrypt("123456"));
		}else{
			qianduoAgencyForm.setPassword(StringUtil.encrypt(qianduoAgencyForm.getPassword()));
		}
		if(null == qianduoAgencyForm.getType() && qianduoAgencyForm.getType() > 0){
			errors.add("请选择机构类型");
		}
		if(StringUtil.isEmpty(qianduoAgencyForm.getPhone())){
			errors.add("电话号码不能为空");
		}
		if(null == qianduoAgencyForm.getRecommendStar()){
			errors.add("评级不能为空");
		}
		if(null == qianduoAgencyForm.getCityID()){
			errors.add("电话号码不能为空");
		}
		if(StringUtil.isEmpty(qianduoAgencyForm.getSummary())){
			errors.add("电话号码不能为空");
		}
		
		String[] productTypes		= qianduoAgencyForm.getProductTypes();
		StringBuilder productTypeSB	= new StringBuilder();
		if(null != productTypes && productTypes.length > 0){
			for(String productType : productTypes){
				productTypeSB.append("|").append(productType);
			}
			productTypeSB.append("|");
		}else{
			errors.add("请选择业务范围");
		}
		qianduoAgencyForm.setProducts(productTypeSB.toString());
		
		if(errors.size() == 0){
			QianduoAgencyServiceImpl service	= (QianduoAgencyServiceImpl)getBean("qianduoAgencyService");
			QianduoAgencyForm agency			= service.getAgencyByUser(qianduoAgencyForm.getUsername());
			if(null != agency){
				errors.add("此用户名已存在");
			}else{			
				boolean ret	= service.saveOrUpdateAgency(qianduoAgencyForm);
				if(!ret){
					errors.add("添加机构信息出错");
				}
			}
		}
		if(errors.size() != 0){
			req.setAttribute("errors", errors);
			return mapping.findForward("addAgency");
		}
		return listAgency(mapping,form,req,res);
	}
}